<template>
  <el-breadcrumb class="app-breadcrumb" separator="/">
    <el-button size="mini" class="el-icon-arrow-left backBtn" @click="historyBack"></el-button>
    <transition-group name="breadcrumb">
      <el-breadcrumb-item v-for="(item,index)  in levelList" :key="item.path" v-if="item.title">
        <span v-if="index==levelList.length-1"
              class="no-redirect">{{item.title}}</span>
        <router-link v-else :to="item.redirect||item.path">{{item.title}}</router-link>
      </el-breadcrumb-item>
    </transition-group>
  </el-breadcrumb>
</template>

<script>
  export default {
    created() {
      this.getBreadcrumb()
    },
    data() {
      return {
        levelList: null
      }
    },
    watch: {
      $route() {
        this.getBreadcrumb()
      }
    },
    methods: {
      getBreadcrumb() {
        let matched = this.$route.matched.filter(item => item.name);

        this.levelList = this.getMatchedList(matched)
      },
      getMatchedList(matchedArr){
        var arr = [];
        matchedArr.forEach(v=>{
          if(v.parent.meta.title){
            if(!arr.some(n=> n.path === v.parent.meta.path)){
              arr.push(v.parent.meta)
            }
          }
          if(v.meta.title){
            if(!arr.some(n=> n.path === v.meta.path)){
              arr.push(v.meta)
            }
          }
        })
        return arr
      },
      historyBack(){
        window.history.back()
      }
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .app-breadcrumb.el-breadcrumb {
    display: inline-block;
    font-size: 14px;
    line-height: 50px;
    padding-left: 10px;
    border-bottom: solid 1px #e6e6e6;
    width: 100%;
    position: fixed;
    top: 50px;
    z-index: 1000;
    background-color: #fff;

  .no-redirect {
    color: #97a8be;
    cursor: text;
  }

  .backBtn {
    padding: 5px;
    margin-right: 12px;
  }

  }
  .open {
    width: 100%;
    -moz-transition: all .28s; /* Firefox 4 */
    -webkit-transition: all .28s; /* Safari and Chrome */
    -o-transition: all .28s; /* Opera */
    transition: all .28s;
    left: 180px;
  }

  .notOpen {
    width: 100%;
    -moz-transition: all .28s; /* Firefox 4 */
    -webkit-transition: all .28s; /* Safari and Chrome */
    -o-transition: all .28s; /* Opera */
    transition: all .28s;
    left: 36px;
  }
</style>
